var SUCCESS	= 0;
var ERR_INPUT_NULL	= 1001;//输入为空
var ERR_PASSWD_NULL = 1002;//密码为空
var ERR_LOGIN_FAILED = 1003;//登录验证失败
var ERR_SIGN_HASH	= 1004;//签名错误

var bActiveX = false;

//哈希算法(const)
var SGD_SM3 = 1;
var SGD_SHA1 = 2;
var SGD_SHA256 = 4;
var hashAlg = SGD_SHA256;
//设置哈希算法，区分sha1和sha256
function ESeaL_SetHashAlg(nAlgCode){
	hashAlg = nAlgCode;
}

//获取证书
function ESeaL_GetUserCert(strCertID, cb) {
	GetSignCert(strCertID, cb);	
}

//获取签章
function ESeaL_GetStampPic(strCertID, cb) {
	GetEsealPic(strCertID, cb);
}

//签名
function ESeaL_SignHashData(strCertID, passwd, hashdata, cb) {
	//获取证书
	var cert = null;	
	GetSignCert(strCertID, function(ret){
		cert = ret.retVal;
		if(cert==null || cert.length == 0)
			return;
	
		//判断证书类型
		var infoType = 3;//获取信息类型，3-证书类型，返回"RSA"或"SM2"
		var infoVal = null;
		GetCertBasicinfo(cert, infoType, function(ret){
			infoVal = ret.retVal;
			if(infoVal==null || infoVal.length == 0)
				return;
			                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
			if(infoVal != "RSA")
				hashAlg = 1;
			
			SignHashData(strCertID, hashdata, hashAlg, cb);
		});
	});		
}

function Ret_Back(ret)
{
	alert(ret);
}
//签名
function ESeaL_ClientSignByHash(json, cur_cert_id, passwd, cb) {
	if(json == "" || cur_cert_id == "") {
		//alert("input error!");
		Ret_Back(ERR_INPUT_NULL);
		return;
	}
	if(!bActiveX){//websocket服务
		if(passwd == null || passwd == ""){
			//alert("passwd error!");
			Ret_Back(ERR_PASSWD_NULL);
			return;
		}
		//login in		
		VerifyUserPIN(cur_cert_id, passwd, function(ret1){
			var isLoginOK = false;
			isLoginOK = ret1.retVal;
			if(!isLoginOK) {
				//alert("login failed!");
				Ret_Back(ERR_LOGIN_FAILED);
				return;
			}
			
			jsonStr = json;
			var jsonObj = JSON.parse(jsonStr);
			
			digestMessageArray = null;
			digestMessageArray = new Array();
			cur_Sign_index = 0;
			
			for(var i = 0; i < jsonObj.digestMessages.length; i++){
				var digestMessage = {}; // 
				digestMessage.signUniqueId = jsonObj.digestMessages[i].signUniqueId;
				digestMessage.fileUniqueId = jsonObj.digestMessages[i].fileUniqueId;
				digestMessageArray[i] = digestMessage;
				ESeaL_SignHashData(cur_cert_id,  passwd, jsonObj.digestMessages[i].hashData,function(ret){
					if(ret.retVal == "") {
						//alert("passwd error");
						Ret_Back(ERR_SIGN_HASH);
						return;
					}
					digestMessageArray[cur_Sign_index].clientSignData = ret.retVal;
					cur_Sign_index++;
					if(cur_Sign_index == jsonObj.digestMessages.length) {
						// all sign ok;
						var digestMessages = {};
						digestMessages.digestMessages = digestMessageArray;
						var retObj = {retVal:(JSON.stringify(digestMessages)), ctx:null};
						cb(retObj);	
					}
				});					
			}
			Ret_Back(SUCCESS);
		});	
		//if login , return.	
		return;
	}else{//控件	
		jsonStr = json;
		var jsonObj = JSON.parse(jsonStr);
		
		digestMessageArray = null;
		digestMessageArray = new Array();
		cur_Sign_index = 0;
		
		for(var i = 0; i < jsonObj.digestMessages.length; i++){
			var digestMessage = {}; // 
			digestMessage.signUniqueId = jsonObj.digestMessages[i].signUniqueId;
			digestMessage.fileUniqueId = jsonObj.digestMessages[i].fileUniqueId;
			digestMessageArray[i] = digestMessage;
			ESeaL_SignHashData(cur_cert_id,  passwd, jsonObj.digestMessages[i].hashData,function(ret){
				if(ret.retVal == "") {
					//alert("passwd error");
					Ret_Back(ERR_SIGN_HASH);
					return;
				}
				digestMessageArray[cur_Sign_index].clientSignData = ret.retVal;
				cur_Sign_index++;
				if(cur_Sign_index == jsonObj.digestMessages.length) {
					// all sign ok;
					var digestMessages = {};
					digestMessages.digestMessages = digestMessageArray;
	
					//var retObj = {retVal:($.toJSON(digestMessages)), ctx:ctx};
					var retObj = {retVal:(JSON.stringify(digestMessages)), ctx:null};
					cb(retObj);				
				}
			});					
		}
		Ret_Back(SUCCESS);
	}
}
